package com.example.demo.util;

public class GeneratePrimes {

    public static int[] generatePrimes(int maxValue){
        if (maxValue >= 2 ){
            int s = maxValue + 1;

            boolean[] f = new boolean[s];
            int i;
            // initialize array to true
            for (i = 0; i < s; i++) {
                f[i] = true;
            }
            // get rid od know non-primes
            f[0] = f[1] = false;
            // sieve
            int j;
            for (i = 2; i < (s * s) + 1; i++) {
                for (j = 2*i; j < s; j += i) {
                    f[j] = false; // multiple is prime
                }
            }
            // how many primes are there?
            int count = 0;
            for (i = 0;i < s; i++){
                if (f[i]){
                    count++;
                }
            }
            int[] primes = new int[count];
            // move the primes into the result
            for (i = 0,j = 0;i < s;i++){
                if (f[i]){
                    primes[j++] = i;
                }
            }
            return primes;
        } else {
            return new int[0];
        }
    }

    public static void main(String[] args) {
        int[] ints = generatePrimes(200);
        for (int i : ints) {
            System.out.println(i);
        }
        System.out.println("total:"+ints.length);
    }
}
